package com.lovehena;

import cn.hutool.core.util.StrUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyCalculator {

    private static String firstNum;
    private static String operator;
    private static TextField textField;

    public static void main(String[] args) {

        // 1 构建窗口
        Frame frame = new Frame("计算器");
        frame.addWindowListener(new ExitWindowAdapter()); // 绑定窗口退出事件

        // 构建panel+textfield
        Panel p1 = new Panel();
        textField = new TextField(30);
        p1.add(textField);

        // 构建网格布局的panel 网格3行5列 每一格中都是按钮
        Panel p2 = new Panel();
        p2.setLayout(new GridLayout(4, 5, 10, 10));
        for (int i = 0; i < 10; i++) {
            Button btn = new Button(i + "");
            btn.addActionListener(new NumberActionListener());
            p2.add(btn);
        }

        initOperatorBtn(p2);
        // 2个 panel放入窗口中
        frame.add(p1, BorderLayout.NORTH); // frame默认是BorderLayout
        frame.add(p2, BorderLayout.CENTER);

        // 宽高自适应
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * 向panel中添加 + - * / clear = 6个按钮 并绑定点击事件
     */
    private static void initOperatorBtn(Panel panel) {
        String[] otherBtns = {"clear", "="};
        String[] operators = {"+", "-", "*", "/"};
        //添加 clear = 2个按钮到panel中 并绑定点击事件
        for (int i = 0; i < otherBtns.length; i++) {
            Button btn = new Button(otherBtns[i]);
            if (otherBtns[i].equals("=")) {
                // = 号的点击事件
                btn.addActionListener(new EqualsActionListener());
            } else if (otherBtns[i].equals("clear")) {
                // 清空按钮点击逻辑
                btn.addActionListener(new ClearActionListener());
            }
            panel.add(btn);
        }
        // + - * / 4个按钮添加到panel中 并绑定点击事件
        for (int i = 0; i < operators.length; i++) {
            Button btn = new Button(operators[i]);
            btn.addActionListener(new OperatorActionListener(operators[i]));
            panel.add(btn);
        }
    }

    private static class NumberActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText(textField.getText() + e.getActionCommand()); // e.getActionCommand()就是点击的数字
        }
    }

    private static class OperatorActionListener implements ActionListener {
        private String currentOperator;

        public OperatorActionListener(String currentOperator) {
            this.currentOperator = currentOperator;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            firstNum = textField.getText();
            operator = this.currentOperator;
            textField.setText("");
        }
    }

    private static class ClearActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            firstNum = "";
            operator = "";
            textField.setText("");
        }
    }

    private static class EqualsActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取第二个数字
            String secondNumber = textField.getText();
            if (StrUtil.isBlank(firstNum) || StrUtil.isBlank(secondNumber)) return;
            try {
                double num1 = Double.parseDouble(firstNum);
                double num2 = Double.parseDouble(secondNumber);
                double result = 0;

                // 根据操作符执行计算
                switch (operator) {
                    case "+":
                        result = num1 + num2;
                        break;
                    case "-":
                        result = num1 - num2;
                        break;
                    case "*":
                        result = num1 * num2;
                        break;
                    case "/":
                        result = num1 / num2;
                        break;
                    default:
                        textField.setText("未知操作符");
                        return;
                }

                // 显示结果
                textField.setText(String.valueOf(result));
            } catch (NumberFormatException ex) {
                textField.setText("输入错误");
            }

            // 重置状态
            firstNum = "";
            operator = "";
        }
    }

    private static class ExitWindowAdapter extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0); // 点击打开的窗口的右上角的x号时退出
        }
    }
}
